
#include "zen_vap_helper.h"

#if ZEN_APP_GLES

#include "zen_vap.h"

namespace Zen { namespace Vap {

	using namespace GL;

	void Helper::beginRender()
	{
		Render::Get()->clear();
	}
	void Helper::endRender()
	{
		
	}
	void Helper::initRender()
	{
		Render::Get()->setClockwiseAsFront(false);
	}

	void Helper::setClearColor(Color4f const & color)
	{
		Render::Get()->setClearColor(color);
	}
	
	void Helper::setBlend(eBlend blend)
	{
		auto render = Render::Get();
		
		switch (blend)
		{
			case eBlend::Normal:
			{
				render->enableBlend();
				render->setBlendFunction
				(GL::eSrcBlend::SrcAlpha,
				 GL::eDstBlend::OneMinusSrcAlpha);
			} break;
			case eBlend::Add:
			{
				render->enableBlend();
				render->setBlendFunction
				(GL::eSrcBlend::SrcAlpha,
				 GL::eDstBlend::One);
			} break;
			case eBlend::None:
			{
				render->disableBlend();
			} break;
			default: break;
		}
	}

	void Helper::setFace(eFace face)
	{
		Render::Get()->cullFace(face);
	}

}}

#elif ZEN_APP_METAL

#include "zen_metal_prepare.h"
#include "zen_vap.h"

namespace Zen { namespace Vap {
	Blends Helper::getBlends(eBlend blend)
	{
		if(blend == eBlend::Inherit)
		{
			blend = RenderStatusStack::Get()->getTopBlend();
		}
		switch (blend) {
			case eBlend::Normal:
				return {
					Metal::eBF::SourceAlpha,
					Metal::eBF::OneMinusSourceAlpha,
					Metal::eBF::SourceAlpha,
					Metal::eBF::OneMinusSourceAlpha
				};
				break;
			case eBlend::Add:
				return {
					Metal::eBF::SourceAlpha,
					Metal::eBF::One,
					Metal::eBF::SourceAlpha,
					Metal::eBF::One
				};
				break;
			case eBlend::Inverse:
				return {
					Metal::eBF::One,
					Metal::eBF::SourceAlpha,
					Metal::eBF::One,
					Metal::eBF::SourceAlpha,
				};
				break;
			case eBlend::None:
			default:
				return {
					Metal::eBF::None,
					Metal::eBF::None,
					Metal::eBF::None,
					Metal::eBF::None
				};
		}
	}
//
//	void DrawBufferTexture::createVBuffer4()
//	{
//		MTVBufIN_T coords[] = {
//			{ { 0, 0, }, { 0, 1, }, },
//			{ { 1, 0, }, { 1, 1, }, },
//			{ { 0, 1, }, { 0, 0, }, },
//			{ { 1, 1, }, { 1, 0, }, },
//		};
//		this->v.updateBuffer(coords, 4);
//	}
	void DrawBufferTexture::setVBufferRect4(float x0, float y0, float x1, float y1)
	{
		MTVBufIN_T coords[] = {
			{ { 0, 0, }, { x0, y1, }, },
			{ { 1, 0, }, { x1, y1, }, },
			{ { 0, 1, }, { x0, y0, }, },
			{ { 1, 1, }, { x1, y0, }, },
		};
		this->v.updateBuffer(coords, 4);
	}

	void Helper::initRender()
	{
		Render::Get()->setClockwiseAsFront(false);
	}
	void Helper::beginRender()
	{
		Metal::Render::Get()->start();
	}

	void Helper::endRender()
	{
		Metal::Render::Get()->end();
	}
	
	void Helper::setClearColor(Color4f const & color)
	{
		Metal::Render::Get()->setClearColor(color);
	}
}}

#endif
